<style>
.align-right-middle
{
	text-align: right; 
	vertical-align: middle
}
</style>
<script>
    jQuery("document").ready(function($){
        canvasScrollTo(0,0);
    });
</script>

<?php echo $this->partial('message.phtml',array('mgs' => Core_Util::getLatestMessage('manage_publish'))) ?>
<?php if (count($this->paginator)): ?>
<table class="sm_tbl_classic" id="list_contest">
<thead>
	<tr>
		<th style="width:150px;">
			<?php echo $this->partial('manage/sortable_item_control.phtml',array(
				'column' => array(
					'sort_column'   => 'title',
					'sort_default'  => 'ASC'
				),
				'label' => 'TABLE_HEADER_CONTEST_NAME'
			)) ?>
		</th>
		<th style="width:150px;">
			<span>
				<?php echo $this->translate('TABLE_HEADER_FANPAGES') ?>
			</span>
		</th>
		<th>
			<?php echo $this->partial('manage/sortable_item_control.phtml',array(
				'column' => array(
					'sort_column'   => 'date_start',
					'sort_default'  => 'DESC',
				),
				'is_default'	=> true,
				'label'			=> 'TABLE_HEADER_DATE_START'
			)) ?>
		</th>
		<th>
			<?php echo $this->partial('manage/sortable_item_control.phtml',array(
				'column' => array(
					'sort_column'   => 'date_end',
					'sort_default'  => 'DESC'
				),
				'label' => 'TABLE_HEADER_DATE_END'
			)) ?>
		</th>
		<th>
			<?php echo $this->partial('manage/sortable_item_control.phtml',array(
				'column' => array(
					'sort_column'   => 'registrants',
					'sort_default'  => 'DESC'
				),
				'label' => 'TABLE_HEADER_REGISTRANTS'
			)) ?>
		</th>
		<th>
			<?php echo $this->partial('manage/sortable_item_control.phtml',array(
				'column' => array(
					'sort_column'   => 'submissions',
					'sort_default'  => 'DESC'
				),
				'label' => 'TABLE_HEADER_SUBMISSIONS'
			)) ?>
		</th>
		<th>
			<span>
				<?php echo $this->translate('TABLE_HEADER_STATUS')?>
			</span>
		</th>
		<th>
			<span>
				<?php echo $this->translate('TABLE_HEADER_ACTIONS') ?>
			</span>
		</th>
	</tr>
</thead>
<tbody>
	<?php
		foreach ($this->paginator->getCurrentItems() as $contest):
		/*@var $contest Core_Model_Contest */
	?>
	<tr>
		<td>
			<p>
				<?php echo $this->escape($contest->getTitle()) ?>
			</p>
		</td>
		<td>
			<?php
				$arrPages = array_combine(explode(',',$contest->id_pages),explode('^',$contest->name_pages));
				echo $this->social->render('listFanpages',array('arrPages' => $arrPages,'view' => $this));
			?>
		</td>
        <td class="date">
			<?php echo $contest->getStartDate() ?>
		</td>
		<td class="date">
			<?php echo $contest->getEndDate() ?>
		</td>
		<td class="align-right-middle"><?php echo $contest->getRegistrantNumber() ?></td>
		<td>
			<div class="align-right-middle">
                <a class="number_entry" href="<?php echo $this->urlKeepIdPage(array('controller' => 'entry','action' => 'list', 'id_contest' => $contest->id)) ?>">
                    <?php echo $contest->getSubmissionNumber() ?>
                </a>
            </div>
		</td>
        <td class="text_capitalize"><?php echo $this->translate($contest->getStatusLabel());?></td>
		<td>
			<select onchange="performAction.call(this,<?php echo $contest->id;?>)">
				<option value=""><?php echo $this->translate('ACTION_LABEL'); ?></option>
				<option value="edit" ><?php echo $this->translate('ACTION_EDIT'); ?></option>
				<?php if ($contest->status != Core_Model_Contest::STATUS_HIDDEN):?>
					<option value="hide"><?php echo $this->translate('ACTION_HIDE'); ?></option>
				<?php else:?>
					<option value="publish"><?php echo $this->translate('ACTION_PUBLISH'); ?></option>
				<?php endif;?>
				<option value="export" ><?php echo $this->translate('ACTION_VIEW_REPORT'); ?></option>
                <option value="entries" ><?php echo $this->translate('ACTION_VIEW_ENTRIES'); ?></option>
			</select>
		</td>
	</tr>
	<?php endforeach;?>
</tbody>
</table>
<?php echo $this->paginationControl(
		$this->paginator,
		'Sliding',
		'manage/pagination_control.phtml',
		array(
			'param_page'    => PARAM_PAGE_NAME,
			'query'         => isset($_GET[PARAM_SORT_DEFAULT])?array(PARAM_SORT_DEFAULT => $_GET[PARAM_SORT_DEFAULT]):array()
		)
	);
?>
<?php else:?>
<div class="sm_paginator_item_empty">
    <p><?php echo $this->translate('MSG_NO_CONTEST');?> - <a  href="<?php echo $this->baseUrl('/contest/manage/create-basic')?>">Create a contest</a></p>
</div>
<?php endif;?>

<script language="javascript">
	function performAction(id)
	{
		var view_state = '<?php echo $this->{Core_Util::VIEW_STATE_MANAGE}?>';
		setcookie('<?php echo Core_Util::VIEW_STATE_MANAGE;?>',view_state,(new Date()).getTime()+3600,'/');
		var action = this.value;
		switch (action)
		{
			case 'edit':
				location.href="<?php echo $this->urlKeepIdPage(array('controller' => 'manage','action' => 'create-basic')) ?>" + '/id/' + id;
				break;
			case 'publish':
				var ok = confirm('<?php echo $this->translate('MSG_CONFIRM_PUBLISH')?>');
				if (!ok) break;
				location.href="<?php echo $this->baseUrl('/contest/manage/publish/id/');?>" + id;
				break;
			case 'hide':
				var ok = confirm('<?php echo $this->translate('MSG_CONFIRM_HIDE')?>');
				if (!ok) break;
				location.href="<?php echo $this->baseUrl('/contest/manage/hide/id/');?>" + id;
				break;
			case 'export':
				location.href="<?php echo $this->urlKeepIdPage(array('controller' => 'report','action' => 'index')) ?>" + '/id/' + id;
				break;
            case 'entries':
				location.href="<?php echo $this->urlKeepIdPage(array('controller' => 'entry','action' => 'list')) ?>" + '/id_contest/' + id;
				break;
		}
		this.selectedIndex = 0;
	}
</script>